package game;

import java.util.Iterator;
import java.util.Vector;

import game.LocationDescriptor;

public class Location {
	String name;
	String desc;
	String image;
	Vector<LocationDescriptor> links;
	Vector<Door> doors;
	Vector<Item> itemsHere;
	
	int linkCount;
	
	public Location() {
		name = "";
		desc = "";
		image = "";
		links = new Vector<LocationDescriptor>();
		itemsHere = new Vector<Item>();
		doors = new Vector<Door>();
		linkCount = 0;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public void setImage(String image) {
		this.image = image;
	}
	
	public void setDesc(String desc) {
		this.desc = desc;
	}

	public void addItem(Item currentItem) {
		itemsHere.add(currentItem);
	}
	
	public void addDoor(Door door) {
		doors.add(door);
	}

	public boolean removeItem(Item item) {
		boolean removeItem = false;
		if (itemsHere.indexOf(item) != -1) {
			itemsHere.remove(itemsHere.indexOf(item));
			removeItem = true;
		} else {
			removeItem = false;
		}
		return removeItem;
	}
	
	public void addLink(String direction, String location) {
		links.add(new LocationDescriptor(direction,location));
	}
	
	public String showLinks() {
		StringBuilder str = new StringBuilder();
		Iterator<LocationDescriptor> iterator = links.iterator();
		while(iterator.hasNext()) {
			LocationDescriptor tmp = iterator.next();
			str.append(tmp.direction + " -> " + tmp.location + "\n");
		}
		return str.toString();
	}
	
	public String showDoors() {
		StringBuilder str = new StringBuilder();
		Iterator<Door> iterator = doors.iterator();
		System.out.println("doors.size(): " + doors.size());
		while(iterator.hasNext()) {
			Door tmp = iterator.next();
			str.append(tmp.direction + "\n" + tmp.open + "\n" + tmp.close + "\n" + tmp.reqItem.name + "\n" + tmp.isOpened + "\n\n");
		}
		return str.toString();
	}
	
	public boolean isDirection(String direction) {
		LocationDescriptor tmp_LD = new LocationDescriptor("","");
		Iterator<LocationDescriptor> iterator = links.iterator();
		System.out.println("links.size(): " + links.size());
		while (iterator.hasNext()) {
			tmp_LD = iterator.next();
			if (tmp_LD.direction.equalsIgnoreCase(direction)) {
				return true;
			}
		}
		return false;
	}
	
	public String getDirection(String direction) {
		LocationDescriptor tmp_LD = new LocationDescriptor("","");
		Iterator<LocationDescriptor> iterator = links.iterator();
		while (iterator.hasNext()) {
			tmp_LD = iterator.next();
			if (tmp_LD.direction.equalsIgnoreCase(direction)) {
				return tmp_LD.location;
			}
		}
		return "INVALID_DIRECTION";
	}
	
	public boolean isDoorOpen(String direction, Inventory playerInventory) {
		if (!doors.isEmpty()) {
			for (int i = 0; i < doors.size(); i++) {
				if (doors.elementAt(i).direction.equalsIgnoreCase(direction)) {
					if (playerInventory.hasItem(doors.elementAt(i).reqItem.name)) {
						doors.elementAt(i).setIsOpened(true);
						return true;
					} else {
						return false;
					}
				}
			}
		}
		return true;
	}
	
	public String getLook() {
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			if ('_' == (name.charAt(i))) {
				str.append(' ');
			}  else {
				str.append(name.charAt(i));
			}
		}
		str.append("\n");
		str.append(desc+"\n");
		if (!doors.isEmpty()) {
			Iterator<Door> iterator = doors.iterator();
			while(iterator.hasNext()) {
				Door tmp = iterator.next();
				if (tmp.isOpened) {
					str.append(tmp.open);
				} else {
					str.append(tmp.close);
				}
				str.append("\n");
			}
		}
		if (!itemsHere.isEmpty()) {
			Iterator<Item> iterator = itemsHere.iterator();
			str.append("\nHere you see a ");
			while(iterator.hasNext()) {
				Item tmp = iterator.next();
				str.append(tmp.dispName);
				if (iterator.hasNext()) {
					str.append(", ");
				}
			}
			str.append(" here.\n");
		}
		
		if (!links.isEmpty()) {
			Iterator<LocationDescriptor> iterator = links.iterator();
			str.append("Exits are ");
			while(iterator.hasNext()) {
				LocationDescriptor tmp_LD = iterator.next();
				str.append(tmp_LD.direction);
				if (iterator.hasNext()) {
					str.append(", ");
				}
			}
		}
		str.append(".");
		str.append("");
		return str.toString();
	}
}
